{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import openpyxl\n",
    "import requests\n",
    "# import json\n",
    "# import xlrd\n",
    "# import xlwt\n",
    "import openpyxl\n",
    "# from lxml import etree"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def read(excel_name):\n",
    "    address_list=[]\n",
    "    wb=openpyxl.load_workbook(excel_name)\n",
    "    sheet=wb['Sheet1']\n",
    "    # print(type(sheet.rows))\n",
    "    for i in sheet.rows:\n",
    "        # print(i[0])\n",
    "        # print(i[0].value)\n",
    "        address_list.append(i[0].value)\n",
    "    return  address_list\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def write(excel_name):"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "def getcode(address):\n",
    "\n",
    "    base_url = \"http://api.map.baidu.com/place/v2/search?query=\" + address + \"&region=珠海&output=json&ak=uwdrLZeQgWir9SnDLQ4GzWyL3tpAU4vF\"\n",
    "\n",
    "    # base_url = \"http://api.map.baidu.com/place/v2/search?query=潮合汇&region=珠海&output=json&ak=uwdrLZeQgWir9SnDLQ4GzWyL3tpAU4vF\"\n",
    "    response = requests.get(base_url)\n",
    "    # print(response)\n",
    "\n",
    "    answer = response.json()\n",
    "    # print(answer)\n",
    "    # print(type(answer))\n",
    "    # print(answer.keys)\n",
    "    # print(answer.keys())\n",
    "    # if 'results'  in answer.keys():\n",
    "\n",
    "    if answer['results']==[]:\n",
    "        return None,None\n",
    "\n",
    "        lat = answer['results'][0]['location']['lat']\n",
    "\n",
    "        lng = answer['results'][0]['location']['lng']\n",
    "        return lat,lng\n",
    "    # else:\n",
    "    #     return None,None"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Ola Lotus莲地', '奥园广场', '奥园广场商圈党群服务中心', '白藤湖', '白藤头', '潮合汇', '诚丰广场', '创新方', '翠前', '翠香路', '大信新都汇']\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "(None, None)\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "None\n",
      "[None, None, None, None, None, (None, None), None, None, None, None, None]\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "'NoneType' object is not subscriptable",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "\u001B[1;32m<ipython-input-81-25879dba3616>\u001B[0m in \u001B[0;36m<module>\u001B[1;34m\u001B[0m\n\u001B[0;32m     10\u001B[0m \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mans_list\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0;32m     11\u001B[0m \u001B[1;32mfor\u001B[0m \u001B[0mtup\u001B[0m \u001B[1;32min\u001B[0m \u001B[0mans_list\u001B[0m\u001B[1;33m:\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[1;32m---> 12\u001B[1;33m     \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtup\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m0\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n\u001B[0m\u001B[0;32m     13\u001B[0m     \u001B[0mprint\u001B[0m\u001B[1;33m(\u001B[0m\u001B[0mtup\u001B[0m\u001B[1;33m[\u001B[0m\u001B[1;36m1\u001B[0m\u001B[1;33m]\u001B[0m\u001B[1;33m)\u001B[0m\u001B[1;33m\u001B[0m\u001B[1;33m\u001B[0m\u001B[0m\n",
      "\u001B[1;31mTypeError\u001B[0m: 'NoneType' object is not subscriptable"
     ]
    }
   ],
   "source": [
    "excel_name='C:/Users/Administrator/Desktop/2.xlsx'\n",
    "address_list=read(excel_name)\n",
    "ans_list=[]\n",
    "print(address_list)\n",
    "for add in address_list:\n",
    "    ans=getcode(add)\n",
    "    time.sleep(0.5)\n",
    "    print(ans)\n",
    "    ans_list.append(ans)\n",
    "print(ans_list)\n",
    "for tup in ans_list:\n",
    "    print(tup[0])\n",
    "    print(tup[1])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "name": "pycharm-a2eace9b",
   "language": "python",
   "display_name": "PyCharm (neuralnrtwork)"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}